package com.example.app.view.c1;

import android.app.Activity;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.view.Surface;

import com.example.app.activity.MLog;
import com.example.app.manager.c1.Camera1Manager;
import com.example.app.view.PreviewBaseView;

/**
 * Created by 郭敏 on 2018/3/21 0021.
 */
public class PreviewC1View extends PreviewBaseView {
    private String tag = "PreviewC1View";

    public PreviewC1View(Context context) {
        super(context);

    }

    public PreviewC1View(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public PreviewC1View(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    private Camera1Manager manager;

    public void setCameraManager(Camera1Manager manager) {
        this.manager = manager;
    }

    private Camera.Size previewSize;

    public void configureTransform(int viewWidth, int viewHeight, Camera.Size previewSize) {
        this.previewSize = previewSize;
        configureTransform(viewWidth, viewHeight);
    }

    private void configureTransform(int viewWidth, int viewHeight) {
        if (previewSize == null) {
            MLog.e(tag, "旋转失败");
            return;
        }
        Context context = getContext();
        if (!(context instanceof Activity)) {
            MLog.e(tag, "旋转失败");
            return;
        }
        Activity activity = (Activity) context;
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        Matrix matrix = new Matrix();
        RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
        RectF bufferRect = new RectF(0, 0, previewSize.height, previewSize.width);
        float centerX = viewRect.centerX();
        float centerY = viewRect.centerY();
        if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
            bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
            matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
            float scale = Math.max(
                    (float) viewHeight / previewSize.height,
                    (float) viewWidth / previewSize.width);
            matrix.postScale(scale, scale, centerX, centerY);
            matrix.postRotate(90 * (rotation - 2), centerX, centerY);
            MLog.e(tag, "configureTransform 1");
        }
        if (Surface.ROTATION_180 == rotation) {
            matrix.postRotate(180, centerX, centerY);
            MLog.e(tag, "configureTransform 2");
        }
        if (Surface.ROTATION_0 == rotation) {
            matrix.postRotate(90, centerX, centerY);
            MLog.e(tag, "configureTransform 3");
        }
        setTransform(matrix);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        //当SurefaceTexture可用的时候，设置相机参数并打开相机
        manager.setTextureView(this);
        manager.onInitCamera();
        MLog.e(tag, "预览可用 onSurfaceTextureAvailable width：" + width + " height:" + height);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        MLog.e(tag, "预览大小发生改变 onSurfaceTextureSizeChanged");
        configureTransform(width, height);
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        MLog.e(tag, "预览关闭 onSurfaceTextureDestroyed");
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // DLog.e(tag, "预览发生更新 onSurfaceTextureDestroyed");
    }

}
